/*     */ package com.zimbra.cs.account.ldap.upgrade;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.Key.CosBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.accesscontrol.GranteeType;
/*     */ import com.zimbra.cs.account.accesscontrol.InlineAttrRight;
/*     */ import com.zimbra.cs.account.accesscontrol.RightModifier;
/*     */ import com.zimbra.cs.account.accesscontrol.TargetType;
/*     */ import com.zimbra.cs.account.ldap.LdapDIT;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.ldap.IAttributes;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapServerType;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor;
/*     */ import com.zimbra.cs.ldap.ZAttributes;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZSearchScope;
/*     */ import com.zimbra.soap.admin.type.GranteeSelector.GranteeBy;
/*     */ import com.zimbra.soap.type.TargetBy;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class BUG_18277
/*     */   extends UpgradeOp
/*     */ {
/*  52 */   private static String[] sAdminUICompForAllDomainAdmins = { "accountListView", "aliasListView", "DLListView", "resourceListView", "saveSearch" };
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  60 */   private static String[] sAdminUICompForAllGlobalAdmins = { "cartBlancheUI" };
/*     */   
/*     */ 
/*     */ 
/*     */   void doUpgrade()
/*     */     throws ServiceException
/*     */   {
/*  67 */     Set<String> domainAdminIds = new HashSet();
/*  68 */     Set<String> globalAdminIds = new HashSet();
/*     */     
/*  70 */     getAllDomainOrGlobalAdmins(domainAdminIds, globalAdminIds);
/*     */     
/*  72 */     for (String domainAdminId : domainAdminIds) {
/*     */       try {
/*  74 */         Account domainAdmin = this.prov.get(Key.AccountBy.id, domainAdminId);
/*  75 */         if (domainAdmin != null)
/*     */         {
/*     */ 
/*  78 */           Domain domain = this.prov.getDomain(domainAdmin);
/*  79 */           if (domain != null)
/*     */           {
/*     */ 
/*  82 */             this.printer.println("Upgrading domain admin: " + domainAdmin.getName());
/*  83 */             grantRights(domain, domainAdmin);
/*     */           }
/*  85 */         } } catch (ServiceException e) { this.printer.println("Skipped upgrading global admin " + domainAdminId + " (Encountered error: " + e.getMessage() + ")");
/*     */       }
/*     */     }
/*     */     
/*  89 */     for (String globalAdminId : globalAdminIds) {
/*     */       try {
/*  91 */         Account globalAdmin = this.prov.get(Key.AccountBy.id, globalAdminId);
/*  92 */         if (globalAdmin != null)
/*     */         {
/*     */ 
/*  95 */           this.printer.println("Upgrading global admin: " + globalAdmin.getName());
/*  96 */           setGlobalAdminUIComp(globalAdmin);
/*     */         }
/*  98 */       } catch (ServiceException e) { this.printer.println("Skipped upgrading global admin " + globalAdminId + " (Encountered error: " + e.getMessage() + ")");
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   private static class Bug18277Visitor
/*     */     extends SearchLdapOptions.SearchLdapVisitor
/*     */   {
/*     */     private final UpgradeOp upgradeOp;
/*     */     private final String configBranchBaseDn;
/*     */     private final Set<String> domainAdminIds;
/*     */     private final Set<String> globalAdminIds;
/*     */     
/*     */     private Bug18277Visitor(UpgradeOp upgradeOp, String configBranchBaseDn, Set<String> domainAdminIds, Set<String> globalAdminIds)
/*     */     {
/* 114 */       super();
/* 115 */       this.upgradeOp = upgradeOp;
/* 116 */       this.configBranchBaseDn = configBranchBaseDn;
/* 117 */       this.domainAdminIds = domainAdminIds;
/* 118 */       this.globalAdminIds = globalAdminIds;
/*     */     }
/*     */     
/*     */     public void visit(String dn, IAttributes ldapAttrs)
/*     */     {
/*     */       try {
/* 124 */         doVisit(dn, (ZAttributes)ldapAttrs);
/*     */       } catch (ServiceException e) {
/* 126 */         this.upgradeOp.printer.println("entry skipped, encountered error while processing entry at:" + dn);
/* 127 */         this.upgradeOp.printer.printStackTrace(e);
/*     */       }
/*     */     }
/*     */     
/*     */     private void doVisit(String dn, ZAttributes ldapAttrs)
/*     */       throws ServiceException
/*     */     {
/* 134 */       if (dn.endsWith(this.configBranchBaseDn)) {
/* 135 */         return;
/*     */       }
/*     */       
/* 138 */       String globalAdminId = getZimbraIdIfGlobalAdmin(ldapAttrs);
/* 139 */       if (globalAdminId != null) {
/* 140 */         this.globalAdminIds.add(globalAdminId);
/*     */       } else {
/* 142 */         String domainAdminId = getZimbraIdIfDomainOnlyAdmin(ldapAttrs);
/* 143 */         if (domainAdminId != null) {
/* 144 */           this.domainAdminIds.add(domainAdminId);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     private String getZimbraIdIfDomainOnlyAdmin(ZAttributes attrs) throws ServiceException {
/* 150 */       String isAdmin = attrs.getAttrString("zimbraIsAdminAccount");
/* 151 */       String isDomainAdmin = attrs.getAttrString("zimbraIsDomainAdminAccount");
/* 152 */       String isDelegatedAdmin = attrs.getAttrString("zimbraIsDelegatedAdminAccount");
/*     */       
/* 154 */       if (("TRUE".equals(isDomainAdmin)) && (!"TRUE".equals(isAdmin)) && (!"TRUE".equals(isDelegatedAdmin)))
/*     */       {
/*     */ 
/* 157 */         return attrs.getAttrString("zimbraId");
/*     */       }
/* 159 */       return null;
/*     */     }
/*     */     
/*     */     private String getZimbraIdIfGlobalAdmin(ZAttributes attrs) throws ServiceException {
/* 163 */       String isAdmin = attrs.getAttrString("zimbraIsAdminAccount");
/*     */       
/* 165 */       if ("TRUE".equals(isAdmin)) {
/* 166 */         return attrs.getAttrString("zimbraId");
/*     */       }
/* 168 */       return null;
/*     */     }
/*     */   }
/*     */   
/*     */   private void getAllDomainOrGlobalAdmins(Set<String> domainAdminIds, Set<String> globalAdminIds) throws ServiceException
/*     */   {
/* 174 */     LdapDIT dit = this.prov.getDIT();
/* 175 */     String[] returnAttrs = { "objectClass", "zimbraId", "zimbraIsAdminAccount", "zimbraIsDomainAdminAccount", "zimbraIsDelegatedAdminAccount" };
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 181 */     String configBranchBaseDn = dit.configBranchBaseDN();
/* 182 */     String base = dit.mailBranchBaseDN();
/* 183 */     String query = "(&(objectclass=zimbraAccount)(|(zimbraIsDomainAdminAccount=TRUE)(zimbraIsAdminAccount=TRUE)))";
/*     */     
/* 185 */     ZLdapContext zlc = null;
/*     */     try {
/* 187 */       zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UPGRADE);
/*     */       
/* 189 */       Bug18277Visitor visitor = new Bug18277Visitor(this, configBranchBaseDn, domainAdminIds, globalAdminIds, null);
/*     */       
/* 191 */       SearchLdapOptions searchOpts = new SearchLdapOptions(base, getFilter(query), returnAttrs, 0, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
/*     */       
/*     */ 
/*     */ 
/* 195 */       zlc.searchPaged(searchOpts);
/*     */     } catch (ServiceException e) {
/* 197 */       throw ServiceException.FAILURE("unable to list all objects", e);
/*     */     } finally {
/* 199 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   private void grantRights(Domain domain, Account domainAdmin)
/*     */     throws ServiceException
/*     */   {
/* 207 */     HashMap<String, Object> attrs = new HashMap();
/* 208 */     attrs.put("zimbraIsDelegatedAdminAccount", "TRUE");
/* 209 */     this.prov.modifyAttrs(domainAdmin, attrs);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 214 */     this.prov.grantRight(TargetType.domain.getCode(), TargetBy.id, domain.getId(), GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.id, domainAdmin.getId(), null, "domainAdminConsoleRights", RightModifier.RM_CAN_DELEGATE);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 221 */     grantCosRights(domain, domainAdmin);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 226 */     this.prov.grantRight(TargetType.global.getCode(), null, null, GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.id, domainAdmin.getId(), null, "listZimlet", RightModifier.RM_CAN_DELEGATE);
/*     */     
/*     */ 
/*     */ 
/* 230 */     this.prov.grantRight(TargetType.global.getCode(), null, null, GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.id, domainAdmin.getId(), null, "getZimlet", RightModifier.RM_CAN_DELEGATE);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 237 */     setDomainAdminUIComp(domainAdmin);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 242 */     long maxQuota = domainAdmin.getLongAttr("zimbraDomainAdminMaxMailQuota", -1L);
/* 243 */     if (maxQuota == -1L) {
/* 244 */       this.prov.grantRight(TargetType.domain.getCode(), TargetBy.id, domain.getId(), GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.id, domainAdmin.getId(), null, InlineAttrRight.composeSetRight(TargetType.account, "zimbraMailQuota"), RightModifier.RM_DENY);
/*     */     }
/*     */   }
/*     */   
/*     */   private void grantCosRights(Domain domain, Account domainAdmin)
/*     */     throws ServiceException
/*     */   {
/* 251 */     Set<String> allowedCoses = domain.getMultiAttrSet("zimbraDomainCOSMaxAccounts");
/*     */     
/* 253 */     for (String c : allowedCoses) {
/* 254 */       String[] parts = c.split(":");
/* 255 */       if (parts.length == 2)
/*     */       {
/* 257 */         String cosId = parts[0];
/*     */         
/*     */ 
/* 260 */         Cos cos = this.prov.get(Key.CosBy.id, cosId);
/* 261 */         if (cos == null) {
/* 262 */           this.printer.println("    cannot find cos " + cosId + ", skipping granting cos right to " + domainAdmin.getName());
/*     */         }
/*     */         else
/*     */         {
/* 266 */           this.prov.grantRight(TargetType.cos.getCode(), TargetBy.id, cosId, GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.id, domainAdmin.getId(), null, "listCos", RightModifier.RM_CAN_DELEGATE);
/*     */           
/*     */ 
/*     */ 
/* 270 */           this.prov.grantRight(TargetType.cos.getCode(), TargetBy.id, cosId, GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.id, domainAdmin.getId(), null, "getCos", RightModifier.RM_CAN_DELEGATE);
/*     */           
/*     */ 
/*     */ 
/* 274 */           this.prov.grantRight(TargetType.cos.getCode(), TargetBy.id, cosId, GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.id, domainAdmin.getId(), null, "assignCos", RightModifier.RM_CAN_DELEGATE);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void setDomainAdminUIComp(Account domainAdmin) throws ServiceException
/*     */   {
/* 282 */     setAdminUIComp(domainAdmin, sAdminUICompForAllDomainAdmins);
/*     */   }
/*     */   
/*     */   private void setGlobalAdminUIComp(Account globalAdmin) throws ServiceException {
/* 286 */     setAdminUIComp(globalAdmin, sAdminUICompForAllGlobalAdmins);
/*     */   }
/*     */   
/*     */   private void setAdminUIComp(Account admin, String[] adminUIComp) throws ServiceException
/*     */   {
/* 291 */     String attrName = "zimbraAdminConsoleUIComponents";
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 300 */     Map<String, Object> attrs = new HashMap();
/* 301 */     attrs.put("+" + attrName, adminUIComp);
/*     */     
/* 303 */     this.prov.modifyAttrs(admin, attrs);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/upgrade/BUG_18277.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */